// 删除用户

// swagger 文档
defineRouteMeta({
  openAPI: {
    description: '删除用户',
    summary: '删除用户',
    tags: ['用户'],
    parameters: [
      {
        name: 'id',
        in: 'path',
        description: '用户id',
        required: true,
        schema: {
          type: 'number'
        }
      }
    ],
    responses: {
      200: {
        description: '用户删除成功',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 200
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      },
      404: {
        description: '用户不存在',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 404
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      },
      500: {
        description: '用户删除失败',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 500
                },
                message: {
                  type: 'string',
                  description: '错误信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      }
    }
  }
});

import { and, eq } from 'drizzle-orm';

export default eventHandler(async event => {
  const { id } = getRouterParams(event);
  const { result } = new useResult(event);
  const deletedUser = await useDrizzle()
    .delete(tables.users)
    .where(and(eq(tables.users.id, Number(id))))
    .returning();

  if (deletedUser.length === 0) {
    return result(null, '未找到用户', 404);
  }
  return result(null, '用户删除成功', 200);
});
